package io.renren.modules.pdf.util;

import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import io.renren.modules.common.dao.EducationDao;
import io.renren.modules.common.entity.EducationEntity;
import io.renren.modules.pdf.TBCell;
import io.renren.modules.pdf.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StyleUtil {


//    private static EducationDao  educationDao;
//
//    @Autowired
//    public static void setEducationDao(EducationDao educationDao) {
//        StyleUtil.educationDao = educationDao;
//    }

    public static final String splitVline = "    |    ";
    public static final String TAB = "    ";
    //判断是否为16进制颜色
    public static boolean isHexColor(String color) {
        String HEX_PATTERN = "[0-9A-Fa-f]+";
        return color != null && color.length() == 6 && color.matches(HEX_PATTERN);
    }
    //将16进制颜色转换为Color对象
    public static BaseColor myColor(String hexColor) {
        hexColor = hexColor.replace("#", "");
        if(hexColor.length()==3) {
            hexColor = hexColor+hexColor;
        }
        if(!isHexColor(hexColor)) {
            hexColor = "ffffff";
        }
        int r= Integer.valueOf( hexColor.substring( 0, 2 ), 16 );
        int g= Integer.valueOf( hexColor.substring( 2, 4 ), 16 );
        int b= Integer.valueOf( hexColor.substring( 4, 6 ), 16 );
        return new BaseColor(r,g,b);
    }
    /**
     * 设置字体，及大小
     * @param size
     * @param style
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    public static Font myfont(float size, int style) throws DocumentException, IOException {
        BaseFont bfChinese = BaseFont.createFont("D:\\我的文档\\syst.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        com.itextpdf.text.Font myFont = new com.itextpdf.text.Font(bfChinese, size, style);
        return myFont;
    }
    public static Font myfont(float size,int style,String color) throws DocumentException, IOException {
        BaseFont bfChinese = BaseFont.createFont("D:\\我的文档\\syst.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        com.itextpdf.text.Font myFont = new com.itextpdf.text.Font(bfChinese, size, style);
        myFont.setColor(myColor(color));
        return myFont;
    }
    /**
     * 标题一
     * @param text
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    public static Paragraph h1(String text) throws DocumentException, IOException {
        Paragraph p = new Paragraph(text,myfont(16,Font.BOLD));
        //p.setSpacingBefore(30f);
        return p;
    }
    /**
     * 带前导图的标题
     * @param text 标题文字
     * @return
     * @throws Exception
     */
    public static PdfPTable add_headtitle_1(String text) throws Exception{
        PdfPTable table = new PdfPTable(2);
        // 实现单元格跨页显示
        table.setSplitLate(false);
        table.setSplitRows(true);
        //设置每列宽度比例
        int width_ht1[] = {2,98};
        table.setWidths(width_ht1);
        table.getDefaultCell().setBorder(0);
        //带前导图(方法1)地址
        //String imagePath2 = "/home/hadoop/Pictures/line.png"; //图片为2px*11px
        //Image image21 = Image.getInstance(imagePath2);
        //带前导图(方法2)用Java画
        BufferedImage image = new BufferedImage(2, 11,BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(new Color(83, 202, 195)); // #53cac3
        g.fillRect(0, 0, 2, 11);
        com.itextpdf.text.Image image21 = Image.getInstance(image,null);

        PdfPCell cellHt1img=new PdfPCell();
        cellHt1img.setBorder(0);
        cellHt1img.setImage(image21);
        cellHt1img.setFixedHeight(1);
        cellHt1img.setPaddingTop(5);
        PdfPCell cellHt1Content = new PdfPCell(h1(text));
        cellHt1Content.setBorder(0);
        //cell21.setLeading(5, 1);
        table.addCell(cellHt1img);
        table.addCell(cellHt1Content);
        return table;
    }
    /**
     * 空行
     * @return
     * @throws Exception
     */
    public static Paragraph blankRow() throws Exception{
        Paragraph blankRow = new Paragraph(20f, " ", myfont(20,Font.BOLD));
        return blankRow;
    }

    /**
     * 空行
     * @param leading 行间距
     * @return
     * @throws Exception
     */
    public static Paragraph blankRow(float leading) throws Exception{
        Paragraph blankRow = new Paragraph(leading, " ", myfont(20,Font.BOLD));
        return blankRow;
    }
    public static PdfPCell add_cell(Object obj,float lead) throws Exception{
        PdfPCell cell = new PdfPCell(new Paragraph(obj+"",myfont(12,Font.NORMAL)));
        cell.setBorder(0);
        cell.setLeading(lead, 1);
        return cell;
    }
    public static PdfPCell add_cell(Object obj,String color,float lead) throws Exception{
        color = color==null?"#000000":color;
        PdfPCell cell = new PdfPCell(new Paragraph(obj+"",myfont(12,Font.NORMAL,color)));
        cell.setBorder(0);
        cell.setLeading(lead, 1);
        return cell;
    }
    public static PdfPCell add_cell(Object obj, TBCell tbCell) throws Exception{
        PdfPCell cell = new PdfPCell(new Paragraph(obj+"",myfont(tbCell.getFontSize(),tbCell.getFontStyle(),tbCell.getTextColor())));
        cell.setBorder(tbCell.getBorder());
        cell.setLeading(tbCell.getLead(), 1);
        if(tbCell.getBgcolor()!=null && !"".equals(tbCell.getBgcolor())) {
            cell.setBackgroundColor(myColor(tbCell.getBgcolor()));
        }
        if(tbCell.getColspan()!=0) {
            cell.setColspan(tbCell.getColspan());
        }
        if(tbCell.getRowspan()!=0) {
            cell.setRowspan(tbCell.getRowspan());
        }
        if(tbCell.getBorderColor()!=null && !"".equals(tbCell.getBorderColor())) {
            cell.setBorderColor(myColor(tbCell.getBorderColor()));
        }
        if(tbCell.getIndent()>0) {
            cell.setIndent(tbCell.getIndent());
        }
        return cell;
    }
    public static PdfPCell add_cell_speace(TBCell tbCell) throws Exception{
        PdfPCell cell = new PdfPCell(new Paragraph(" ",myfont(tbCell.getFontSize(),tbCell.getFontStyle(),tbCell.getTextColor())));
        cell.setBorder(tbCell.getBorder());
        cell.setLeading(tbCell.getLead(), 1);
        if(tbCell.getBgcolor()!=null && !"".equals(tbCell.getBgcolor())) {
            cell.setBackgroundColor(myColor(tbCell.getBgcolor()));
        }
        if(tbCell.getColspan()!=0) {
            cell.setColspan(tbCell.getColspan());
        }
        if(tbCell.getRowspan()!=0) {
            cell.setRowspan(tbCell.getRowspan());
        }
        if(tbCell.getBorderColor()!=null && !"".equals(tbCell.getBorderColor())) {
            cell.setBorderColor(myColor(tbCell.getBorderColor()));
        }
        if(tbCell.getIndent()>0) {
            cell.setIndent(tbCell.getIndent());
        }
        return cell;
    }

    /**
     * 工作经验主体方法
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_workExpTable(Document doc, JobExperience jobExperience) throws Exception{

        PdfPTable workExpTable = new PdfPTable(1);
        // 实现单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);
        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs1= TBCell.NEW().bgcolor("#eee").lead(5).borderColor("#eee").fontStyle(Font.BOLD);
        TBCell tbcs2= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).fontStyle(Font.BOLD);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs4= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).indent(20);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);



            workExpTable.addCell(add_cell(jobExperience.getCompanyName(), tbcs1));
            StringBuffer sbf = new StringBuffer();
            sbf.append(jobExperience.getWorkTime());
            sbf.append(splitVline);
            sbf.append(jobExperience.getJobPosition());
            sbf.append(splitVline);
            sbf.append(jobExperience.getExpSalary());
            //sbf.append(splitVline);
            //sbf.append(jobExperience.getMajor());
            workExpTable.addCell(add_cell(sbf.toString(),tbcs4));
            workExpTable.addCell(add_cell("工作描述：",tbcs2));
            StringBuffer sbd = new StringBuffer();
            sbd.append(jobExperience.getWorkDescribe());
            workExpTable.addCell(add_cell(sbd.toString(),tbcs4));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);

    }

    /**
     * 项目经验主体
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_projectExpTable(Document doc, ProjectExperience projectExperience) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        // 实现单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);
        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs1= TBCell.NEW().bgcolor("#eee").lead(5).borderColor("#eee").fontStyle(Font.BOLD);
        TBCell tbcs2= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).fontStyle(Font.BOLD);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs4= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).indent(20);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);

            workExpTable.addCell(add_cell(projectExperience.getProjeftName(), tbcs1));
            StringBuffer sbf = new StringBuffer();
            sbf.append(projectExperience.getProjectTime());
            sbf.append(splitVline);
            sbf.append(projectExperience.getProjectRole());
            sbf.append(splitVline);
            sbf.append(projectExperience.getUnit());
            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell("项目描述：",tbcs2));
            StringBuffer sbd = new StringBuffer();
            sbd.append(projectExperience.getProjectDescribe());
            workExpTable.addCell(add_cell(sbd.toString(),tbcs4));
            workExpTable.addCell(add_cell("个人职责：",tbcs2));
            StringBuffer sbgr = new StringBuffer();
            sbgr.append(projectExperience.getPersonalResponsibility());
            workExpTable.addCell(add_cell(sbgr.toString(),tbcs4));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));
    }

    /**
     * 教育经历
     */
    public static void add_eduExpTable(Document doc,EducationEntityDTO educationEntityDTO) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        // 实现单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);
        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs1= TBCell.NEW().bgcolor("#eee").lead(5).borderColor("#eee").fontStyle(Font.BOLD);
        TBCell tbcs2= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).fontStyle(Font.BOLD);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs4= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).indent(20);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);


        String educationBack = "";
        if ("1".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "小学";
        }else if ("2".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "初中";
        }else if ("3".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "高中";
        }else if ("4".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "专科";
        }else if ("5".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "本科";
        }else if ("6".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "211/985普本";
        }else if ("7".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "硕士";
        }else if ("8".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "博士";
        }else if ("9".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "博士后";
        }else if ("10".equals(educationEntityDTO.getEducationBackground())){
            educationBack = "学历不限";
        }else {
            educationBack = "学历不详";
        }

        workExpTable.addCell(add_cell(educationEntityDTO.getUniversityName(), tbcs1));
            StringBuffer sbf = new StringBuffer();
            sbf.append("专业：" +educationEntityDTO.getMajor());
            sbf.append(splitVline);
            sbf.append("学历：" +educationBack );
            sbf.append(splitVline);
            sbf.append("时间：" + educationEntityDTO.getTimeEducation());
            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));
    }
    /**
     * 培训经历
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_trainExpTable(Document doc,TrainingRecord trainingRecord) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        // 实现单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);
        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs1= TBCell.NEW().bgcolor("#eee").lead(5).borderColor("#eee").fontStyle(Font.BOLD);
        TBCell tbcs2= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).fontStyle(Font.BOLD);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs4= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1).indent(20);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);

            workExpTable.addCell(add_cell(trainingRecord.getTrainingCourse(), tbcs1));
            StringBuffer sbf = new StringBuffer();
            sbf.append("时间：" + trainingRecord.getTrainingTime());
            sbf.append(splitVline);
            sbf.append("培训机构：" + trainingRecord.getTrainingOrganization());
            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));
    }
    /**
     * 专业技能
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_majorSkillExpTable(Document doc,Skillentity skillentity) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        //配置单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);

        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);

            StringBuffer sbf = new StringBuffer();
            sbf.append(skillentity.getSkillName());
            sbf.append(splitVline);

            String skillLeven = "";

            if ("0".equals(skillentity.getLevel())){
                skillLeven = "一般";
            }else if ("1".equals(skillentity.getLevel())){
                skillLeven = "熟练";
            }

            sbf.append(skillLeven);

            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));

    }
    /**
     * 语言能力
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_langSkillExpTable(Document doc,LanguageEntity languageEntity) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        //配置单元格跨页显示
        workExpTable.setSplitLate(false);
        workExpTable.setSplitRows(true);

        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs3= TBCell.NEW().bgcolor("#eee").lead(10).borderColor("#eee").colspan(2).rowspan(1);
        TBCell tbcs5= TBCell.NEW().bgcolor("#eee").lead(1).borderColor("#eee").colspan(2).rowspan(1);

            StringBuffer sbf = new StringBuffer();
            sbf.append(languageEntity.getLanuageName());
            sbf.append(splitVline);

            String speakLevel = "";
            if ("0".equals(languageEntity.getHearAndSpeakLevel())){
                speakLevel = "一般";
            }else if ("1".equals(languageEntity.getHearAndSpeakLevel())){
                speakLevel = "熟练";
            }


            sbf.append("听说能力：" + speakLevel);
            sbf.append(splitVline);

            String writeLevel = "";
            if ("0".equals(languageEntity.getReadAndWriteLevel())){
                writeLevel = "一般";
            }else if ("1".equals(languageEntity.getReadAndWriteLevel())){
                writeLevel = "熟练";
            }
            sbf.append("读写能力：" + writeLevel);
            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));
    }
    /**
     * 资格证书
     * @param doc Document对象
     * @throws Exception
     */
    public static void add_certExpTable(Document doc,CertificateEntityDTO certificateEntityDTO) throws Exception{
        PdfPTable workExpTable = new PdfPTable(1);
        int workExpTableWidth[] = {100};
        workExpTable.setWidths(workExpTableWidth);
        workExpTable.getDefaultCell().setBorder(0);
        TBCell tbcs3= TBCell.NEW().lead(10).colspan(2).rowspan(1);
        TBCell tbcs5= TBCell.NEW().lead(1).colspan(2).rowspan(1);

            StringBuffer sbf = new StringBuffer();
            sbf.append("证书名称：" + certificateEntityDTO.getCertificateName());
            sbf.append(splitVline);
            sbf.append("获得时间：" + certificateEntityDTO.getGainTime());
            workExpTable.addCell(add_cell(sbf.toString(),tbcs3));
            workExpTable.addCell(add_cell_speace(tbcs5));
            workExpTable.addCell(add_cell_speace(tbcs5));
            doc.add(workExpTable);
            doc.add(blankRow(5));
    }

}
